package com.ruoyi.procurementmanagement.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.procurementmanagement.domain.EsProcurementContractProduct;
import com.ruoyi.procurementmanagement.mapper.EsProcurementContractMapper;
import com.ruoyi.procurementmanagement.domain.EsProcurementContract;
import com.ruoyi.procurementmanagement.service.IEsProcurementContractService;

/**
 * 采购合同(es_procurement_contract)Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-05
 */
@Slf4j
@Service
public class EsProcurementContractServiceImpl implements IEsProcurementContractService
{
    @Autowired
    private EsProcurementContractMapper esProcurementContractMapper;

    /**
     * 查询采购合同(es_procurement_contract)
     *
     * @param id 采购合同(es_procurement_contract)主键
     * @return 采购合同(es_procurement_contract)
     */
    @Override
    public EsProcurementContract selectEsProcurementContractById(Integer id)
    {
        return esProcurementContractMapper.selectEsProcurementContractById(id);
    }

    /**
     * 查询采购合同(es_procurement_contract)列表
     *
     * @param esProcurementContract 采购合同(es_procurement_contract)
     * @return 采购合同(es_procurement_contract)
     */
    @Override
    public List<EsProcurementContract> selectEsProcurementContractList(EsProcurementContract esProcurementContract)
    {
        return esProcurementContractMapper.selectEsProcurementContractList(esProcurementContract);
    }

    /**
     * 新增采购合同(es_procurement_contract)
     *
     * @param esProcurementContract 采购合同(es_procurement_contract)
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEsProcurementContract(EsProcurementContract esProcurementContract) {
        esProcurementContract.setCreateTime(DateUtils.getNowDate());
        int rows = esProcurementContractMapper.insertEsProcurementContract(esProcurementContract);
        insertEsProcurementContractProduct(esProcurementContract);
        return rows;
    }

    /**
     * 修改采购合同(es_procurement_contract)
     *
     * @param esProcurementContract 采购合同(es_procurement_contract)
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEsProcurementContract(EsProcurementContract esProcurementContract) {
        // 先删除原有的合同明细
        esProcurementContractMapper.deleteEsProcurementContractProductByContractId(esProcurementContract.getId());

        // 保存新的合同明细
        insertEsProcurementContractProduct(esProcurementContract);

        // 更新合同主表
        return esProcurementContractMapper.updateEsProcurementContract(esProcurementContract);
    }

    /**
     * 批量删除采购合同(es_procurement_contract)
     *
     * @param ids 需要删除的采购合同(es_procurement_contract)主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEsProcurementContractByIds(Integer[] ids)
    {
        esProcurementContractMapper.deleteEsProcurementContractProductByContractIds(ids);
        return esProcurementContractMapper.deleteEsProcurementContractByIds(ids);
    }

    /**
     * 删除采购合同(es_procurement_contract)信息
     *
     * @param id 采购合同(es_procurement_contract)主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEsProcurementContractById(Integer id)
    {
        esProcurementContractMapper.deleteEsProcurementContractProductByContractId(id);
        return esProcurementContractMapper.deleteEsProcurementContractById(id);
    }

    @Override
    public int updateContractStatus(Integer id, String status) {
        try {
            EsProcurementContract contract = esProcurementContractMapper.selectEsProcurementContractById(id);
            if (contract == null) {
                log.warn("合同不存在，ID：{}", id);
                return 0;
            }

            if (!status.equals(contract.getContractStatus())) {
                contract.setContractStatus(status);
                int result = esProcurementContractMapper.updateEsProcurementContract(contract);
                log.info("合同状态更新：ID={}, 旧状态={}, 新状态={}",
                        id, contract.getContractStatus(), status);
                return result;
            }
            return 0;
        } catch (Exception e) {
            log.error("更新合同状态失败：", e);
            throw new ServiceException("更新合同状态失败");
        }
    }

    /**
     * 新增采购合同产品(es_procurement_contract_product)信息
     */
    public void insertEsProcurementContractProduct(EsProcurementContract esProcurementContract) {
        List<EsProcurementContractProduct> productList = esProcurementContract.getEsProcurementContractProductList();
        Integer contractId = esProcurementContract.getId();
        if (productList != null && !productList.isEmpty()) {
            productList.forEach(item -> {
                item.setContractId(contractId);
            });
            esProcurementContractMapper.batchEsProcurementContractProduct(productList);
        }
    }

    @Override
    public List<EsProcurementContractProduct> selectEsProcurementContractProductList(Integer contractId) {
        return esProcurementContractMapper.selectEsProcurementContractProductList(contractId);
    }
}
